//
//  SFSwipeHeaderView.m
//  Test
//
//  Created by xueshan1 on 2019/7/17.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "SFSwipeHeaderView.h"
//#import "UIView+Externsion.h"

static CGFloat rubberBandRate(CGFloat offset) {
    
    const CGFloat constant = 0.15f;
    const CGFloat dimension = 10.0f;
    const CGFloat startRate = 0.85f;
    // 计算拖动视图translation的增量比率，起始值为startRate（此时offset为0）；随着frame超出的距离offset的增大，增量比率减小
    CGFloat result = dimension * startRate / (dimension + constant * fabs(offset));
    return result;
}


@interface SFSwipeHeaderView ()
{
    CGFloat _originY;
}
@property(nonatomic,strong)UIPanGestureRecognizer *panGes;
@property(nonatomic,assign)BOOL isDragging;
@property(nonatomic,assign)CGFloat minY;
@property(nonatomic,assign)CGFloat maxY;
@property(nonatomic,assign)CGFloat newY;
@end

@implementation SFSwipeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
        [self addGestureRecognizer:_panGes];
        _originY = self.y;

    }
    return self;
}

- (void)panGestureAction:(UIPanGestureRecognizer *)panGesture{
    UIGestureRecognizerState state = panGesture.state;
    switch (state) {
        case UIGestureRecognizerStateBegan:
        {
            
        }
            break;
        case UIGestureRecognizerStateChanged:
        {   self.isDragging = YES;
            CGPoint translationP = [panGesture translationInView:self];
            CGFloat newY = self.y + translationP.y;
        
            CGFloat constrainedOrignY = fmax(self.minY, fmin(newY, self.maxY));
            CGFloat rubberBandedRate  = rubberBandRate(newY - constrainedOrignY);
            NSLog(@"header移动:%f 率:%f",translationP.y ,  rubberBandedRate);

            self.newY = self.y + translationP.y * rubberBandedRate;
            [panGesture setTranslation:CGPointMake(translationP.x, 0) inView:self];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {   self.isDragging = NO;
            if (self.y > _originY ) {//向下滑动,都需要复位
                [UIView animateWithDuration:0.25 animations:^{
                    self.newY = self->_originY;
                }];
            }
            NSLog(@"self.y = %f  self.maxY = %f",self.y, self.maxY);
            if (self.y < _originY  && self.y < self.minY) {// 向上滑动, 需要考虑当前tableView超出屏幕外的高度
                [UIView animateWithDuration:0.25 animations:^{
                    self.newY = self->_originY;
                }];
            }
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}
- (void)setNewY:(CGFloat)newY{
    self.y = newY;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(swpipeHeaderViewDidFrameChanged:)]) {
        [self.delegate swpipeHeaderViewDidFrameChanged:self];
    }
}
- (void)setIsDragging:(BOOL)isDragging{
    _isDragging = isDragging;
    if (self.delegate && [self.delegate respondsToSelector:@selector(swipeHeaderViewIsDragging:)]) {
        [self.delegate swipeHeaderViewIsDragging:_isDragging];
    }
}

- (CGFloat)minY{
    _minY = 0;
    if (self.delegate && [self.delegate respondsToSelector:@selector(minY_HeaderViewFrame)]) {
        _minY = [self.delegate minY_HeaderViewFrame];
    }
    return _minY;
}
- (CGFloat)maxY{
    _maxY = 0;
    if (self.delegate && [self.delegate respondsToSelector:@selector(maxY_HeaderViewFrame)]) {
        _maxY = [self.delegate maxY_HeaderViewFrame];
    }
    return _maxY;
}



@end
